var oldAnchor = "";
$(document).ready(function() {
	$(document).keypress(keyPress);

	$('div#nav ul').children().width(133);
	$('div#nav ul').kwicks({max: 320, min:96, duration: 400, easing: 'easeOutQuad', complete: 'callback'});

	function loadPage(anchor,animation){
		var page = anchor.split('-')[0];
		//Gestion des partie de aam
		if(oldAnchor != page){
			var toLoad = page+'.html';
			//Gestion du menu
			$("div#nav ul").children().removeClass("selected");
			$("div#nav ul li a[href=\""+page+".html\"]").parent().addClass("selected");
			$("div#nav ul li a[href=\""+page+".html\"]").parent().trigger("mouseover",animation);

			if(animation!=false){
				$('body').append('<div id="load">LOADING...</div>');
			}
			loadContent(toLoad,animation,anchor);
		}
		else if(page == "aam" && anchor != page){
			showAAMArticle(anchor,500);
		}
		else if(page == "aam" && anchor == page){
			closeAAMArticle(500);
		}
		oldAnchor = page;
	}
	function loadContent(toLoad,animation,anchor) {
		$.get(toLoad,function(data){
			var htdata = jQuery(data);
			var content = htdata.find('div.article').parent().html();
			var title = htdata.find('div#title').html();

			if($('div#main').html() != content){
				if(animation==false){
					$('div#main').html(content);
					if(anchor.slice(0,3) == "aam" && anchor.length > 3){
						showAAMArticle(anchor,0);
					}
					else if(anchor.slice(0,3) == "aam"){
						closeAAMArticle(0);
					}
				}
				else{
					$('div#main').slideUp(500).queue(function(){
						$(this).html(content);
						jQuery.dequeue(this);
						if(anchor.slice(0,3) == "aam" && anchor.length > 3){
							showAAMArticle(anchor,0);
						}
						else if(anchor.slice(0,3) == "aam"){
							closeAAMArticle(0);
						}
					});
				}
			}
			if($('div#title').html() != title){
				if(animation==false){
					$('div#title').html(title);
				}
				else{
					$('div#title').fadeOut(500).queue(function(){
						$(this).html(title);
						$(this).fadeIn(500);
						jQuery.dequeue(this);
					});
				}
			}
			showNewContent(animation);
		});
	}
	function showNewContent(animation) {
		if(animation==false){
			$('div#main').show().queue(function(){
				loadAAMMenuOver();
				updateZoom();
				jQuery.dequeue(this);
			});
		}
		else{
			$('div#main').queue(function(){
				loadAAMMenuOver();
				$(this).slideDown(500);
				updateZoom();
				$('#load').remove();
				jQuery.dequeue(this);
			});
		}
	}

	$(window).hashchange( function(data){
		loadPage(location.hash.slice(1));
		return false;
	});
	if(location.hash.length>0){
		loadPage(location.hash.slice(1),false);
	}
	else{
		$("div#nav ul li.selected").trigger("mouseover",false);
	}

	$('#logo-small').click(function(){
		location.hash = $(this).attr('href').split('.')[0];
		return false;
	});
	
	$('div#nav li a').click(function(){
		location.hash = $(this).attr('href').split('.')[0];
		return false;
	});

	function closeAAMArticle(delay){
		if($('div.toogle')){
			$('div.toogle div.content').slideUp(delay);
			$('div.toogle div.header h1 a img.select').removeClass('select').animate({
				opacity:0
			},750);
			updateIdAAM();
		}
	}

	function showAAMArticle(selected,delay){
		closeAAMArticle(delay);
		var anchor = "#"+selected+"-article";
		$(anchor +' div.content').slideDown(delay);
		$(anchor +' div.header h1 a img').addClass('select');
		scrollToAnchor(anchor,delay);
	}

	function updateIdAAM(){
		$('div.toogle').each(function(){
			if($(this).attr('id').slice(-7) != "article"){
				$(this).attr('id',$(this).attr('id')+"-article");
			}
		});
	}

	closeAAMArticle(0);
	loadAAMMenuOver();
	updateZoom();
});

function updateZoom(){
	$('a.zoom').click(function(){
		$('body').append("<div class=\"none\" id=\"Overlay\"></div>");
		var link = $(this).attr('href');
		img = $('<img alt="Overlay" />').attr('src',link).load(function(){
			$('div#Overlay').append($(this));
			$('div#Overlay').fadeIn(2500);
		}).click(function(){
			$('div#Overlay').fadeOut(2500,function(){
				$(this).remove();
			});
		});
		return false;
	});
}

function loadAAMMenuOver(){
	$('div.toogle div.header a img').each(function(){
		if(!$(this).hasClass('select')){
			$(this).css({
				opacity: 0
			});
		}
	});
	$('div.toogle div.header a').hover(function(){
			var img = $(this).find('img');
			if(!img.hasClass('select')){
				img.stop().animate({
					opacity:1
				},750);
			}
		},
		function(){
			var img = $(this).find('img');
			if(!img.hasClass('select')){
				img.stop().animate({
					opacity:0
				},750);
			}
		}
	);
}
function scrollToAnchor(anchor,delay){
	var index = $(anchor).parent().children().index($(anchor));
	var position;
	if(index == 1){
		position = $(anchor).offset().top;
	}
	else{
		var tmp = $($(anchor).parent().children().get(index-1));
		position = tmp.offset().top + $(tmp.children()[0]).outerHeight();
	}
	$('html,body').animate({
			scrollTop: position
	}, delay);
}

var statusBS= new Array();
statusBS[0]=0;
statusBS[1]=0;
statusBS[2]=0;
statusBS[3]=0;
statusBS[4]=0;
statusBS[5]=0;
statusBS[6]=0;
statusBS[7]=0;
statusBS[8]=0;
statusBS[9]=0;

var linksBS= new Array();
linksBS.push("http://nachodonut.files.wordpress.com/2011/01/dannytannerbobsaget.png");
linksBS.push("http://www.nndb.com/people/453/000025378/bsaget2-sm.jpg");
linksBS.push("http://images.wikia.com/muppet/images/c/c7/Bob_saget.jpg");
linksBS.push("http://ecdn2.hark.com/images/000/000/755/755/original.jpg");
linksBS.push("http://cp91279.biography.com/1120330742/1120330742_1715760922_Biography-Biography-Bob-Saget-Stamos-97650546001.jpg");
linksBS.push("http://i2.listal.com/image/339881/600full-bob-saget.jpg");
linksBS.push("http://www.wildsound-filmmaking-feedback-events.com/images/85bobsaget5.jpg");
linksBS.push("http://cdn2.screenjunkies.com/wp-content/uploads/images/2009/bobsaget.jpg");
linksBS.push("http://www.celebrityring.info/images/pictures/Bob-Saget-2.jpg");
linksBS.push("http://img2.timeinc.net/people/i/2008/news/081124/bob_saget320.jpg");
linksBS.push("http://userserve-ak.last.fm/serve/252/3843997.jpg");
function checkCode(){
	if(
		statusBS[9] == 38 && 
		statusBS[8] == 38 &&
		statusBS[7] == 40 && 
		statusBS[6] == 40 &&
		statusBS[5] == 37 &&
		statusBS[4] == 39 &&
		statusBS[3] == 37 &&
		statusBS[2] == 39 &&
		statusBS[1] == 98 &&
		statusBS[0] == 97 
		){
		return true;
	}
	else{
		return false;
	}
}
function addCode(event){
	statusBS[9] = statusBS[8];
	statusBS[8] = statusBS[7];
	statusBS[7] = statusBS[6];
	statusBS[6] = statusBS[5];
	statusBS[5] = statusBS[4];
	statusBS[4] = statusBS[3];
	statusBS[3] = statusBS[2];
	statusBS[2] = statusBS[1];
	statusBS[1] = statusBS[0];
	statusBS[0] = (event.keyCode==0?event.which:event.keyCode);
}
function keyPress(event){
	addCode(event);
	if(checkCode()){
		$('body').append("<div class=\"none\" id=\"Overlay\"></div>");
		var link = linksBS[Math.floor(Math.random()*linksBS.length)];
		img = $('<img alt="Overlay" />').attr('src',link).load(function(){
			$('div#Overlay').append($(this));
			$('div#Overlay').fadeIn(2500).delay(1000).fadeOut(2500,function(){
				$(this).remove();
			});
		});
	}
}
